package com.internetCafes.spms.common.AIRecognize;

import com.internetCafes.spms.common.CommonConstants;
import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.EntityBuilder;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

/**
 * @author LinYe
 * @date 2020/10/14
 */
@Component
public class QRCodeRecognizeUtils {

    /**
     * 调用腾讯云二维码识别接口，获取二维码中的信息
     * @param FilePath 图片文件路径
     * @return 二维码识别结果
     */
    public static String ScanQRCode(String FilePath){
        String url= CommonConstants.QRCodeRecognizeParam.URL;
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpPut httpPut = new HttpPut(url);
        RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(35000).setConnectionRequestTimeout(35000).setSocketTimeout(60000).build();
        httpPut.setConfig(requestConfig);
        httpPut.setHeader("host", CommonConstants.QRCodeRecognizeParam.HOST);
        SimpleDateFormat sdf = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss 'GMT'", Locale.US);
        String date = sdf.format(new Date());
        httpPut.setHeader("Date", date);
        httpPut.setHeader("Authorization", CommonConstants.QRCodeRecognizeParam.AUTHORIZATION);
        httpPut.setHeader("Pic-Operations", CommonConstants.QRCodeRecognizeParam.PIC_OPERATIONS);
        File file = new File(FilePath);
        EntityBuilder entityBuilder = EntityBuilder.create();
        entityBuilder.setFile(file);
        HttpEntity build = entityBuilder.build();
        httpPut.setEntity(build);
        CloseableHttpResponse httpResponse = null;
        try {
            httpResponse = httpClient.execute(httpPut);
            HttpEntity entity = httpResponse.getEntity();
            String result = EntityUtils.toString(entity, StandardCharsets.UTF_8);
            return result;
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (httpResponse != null) {
                try {
                    httpResponse.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (null != httpClient) {
                try {
                    httpClient.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return "";
    }

    /**
     * 对二维码识别后结果进行处理，截取所需要部分信息。
     * @param scanResult 二维码识别结果
     * @return 处理后结果
     */
    public static String subPrefix(String scanResult){
        String result = scanResult.substring(scanResult.indexOf("<CodeUrl>")+9);
        return result;
    }
    public static String subSuffix(String scanResult){
        String result = scanResult.substring(0, scanResult.indexOf("</CodeUrl>"));
        return result;
    }
}
